package com.tg.app.activity.device.list;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.base.BaseFragment;
import com.tg.app.R;
import com.tange.base.toolkit.StatusBarUtil;
import com.tange.base.toolkit.StatusNavUtils;
import com.tg.message.msg.MessageFragment;

public class HomeFragment extends BaseFragment {

    public static final String TAG = "HomeFragmentTAG";

    private static final int TAB_NONE = -1;
    private static final int TAB_HOME = 0;
    private static final int TAB_MESSAGE = 1;
    private static final int TAB_MINE = 2;
    
    private View rootView;

    private View homeTabView;
    private ImageView homeIcon;
    private TextView homeText;

    private View messageTabView;
    private ImageView messageIcon;
    private TextView messageText;

    private View mineTabView;
    private ImageView mineIcon;
    private TextView mineText;

    private DeviceListFragment deviceListFragment;
    private MessageFragment messageFragment;
    private MineFragment mineFragment;

    private Fragment currentFragment;

    private int currentTabIndex = TAB_NONE;

    private MessageFragment.DeviceListCallback deviceListCallback;

    public static HomeFragment newInstance(Bundle bundle) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        if (bundle != null) {
            args.putAll(bundle);
        }
        fragment.setArguments(args);
        return fragment;
    }

    public void onActivityRestart() {
        if (deviceListFragment != null) {
            deviceListFragment.onActivityRestart();
        }
    }

    public void onNewIntent(Intent intent) {
        if (deviceListFragment != null) {
            deviceListFragment.onNewIntent(intent);
        } else {
            Bundle b = getArguments();
            if (b == null) {
                b = new Bundle();
            }
            b.putAll(intent.getExtras());
        }
    }

    public boolean onBackKeyClicked() {
        if (currentTabIndex == TAB_HOME) {
            if (deviceListFragment != null) {
                return deviceListFragment.onBackKeyClicked();
            }
        }

        if (currentTabIndex == TAB_MINE) {
            if (mineFragment != null) {
                return mineFragment.onBackKeyClicked();
            }
        }

        return false;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (deviceListFragment != null) {
            deviceListFragment.onActivityResult(requestCode, resultCode, data);
        }
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_tange_home_tabs_container, container, false);
        initView();
        return rootView;
    }

    private void initView() {

        homeTabView = rootView.findViewById(R.id.bottom_tab_home_root);
        homeIcon = rootView.findViewById(R.id.bottom_tab_home_icon);
        homeText = rootView.findViewById(R.id.bottom_tab_home_text);

        messageTabView = rootView.findViewById(R.id.bottom_tab_message_root);
        messageIcon = rootView.findViewById(R.id.bottom_tab_message_icon);
        messageText = rootView.findViewById(R.id.bottom_tab_message_text);

        mineTabView = rootView.findViewById(R.id.bottom_tab_mine_root);
        mineIcon = rootView.findViewById(R.id.bottom_tab_mine_icon);
        mineText = rootView.findViewById(R.id.bottom_tab_mine_text);

        homeTabView.setOnClickListener(view -> {
            showTab(TAB_HOME);
        });

        messageTabView.setOnClickListener(view -> {
            showTab(TAB_MESSAGE);
        });

        mineTabView.setOnClickListener(view -> {
            showTab(TAB_MINE);
        });

        // TODO：首次要展示哪个TAB，应该由上层控制
        showTab(TAB_HOME);
    }

    private void showTab(int index) {

        if (currentTabIndex == index) return;

        int statusBarColor;

        if (index == TAB_MESSAGE || index == TAB_MINE) {
            statusBarColor = getResources().getColor(R.color.white);
            StatusBarUtil.setLightStatusBar(getActivity(), true);
        } else {
            statusBarColor = getResources().getColor(R.color.play_background);
            StatusBarUtil.setLightStatusBar(getActivity(), true);
        }

        final FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

        homeIcon.setImageResource(index == TAB_HOME ? R.drawable.ic_tange_global_home_device : R.drawable.ic_tange_global_home_device_gray);
        messageIcon.setImageResource(index == TAB_MESSAGE ? R.drawable.ic_tange_home_alarm : R.drawable.ic_tange_home_alarm_gray);
        mineIcon.setImageResource(index == TAB_MINE ? R.drawable.ic_tange_home_me : R.drawable.ic_tange_home_me_gray);

        homeText.setTextColor(getResources().getColor(index == TAB_HOME ? R.color.global_text_color : R.color.global_message_noselected_color));
        messageText.setTextColor(getResources().getColor(index == TAB_MESSAGE ? R.color.global_text_color : R.color.global_message_noselected_color));
        mineText.setTextColor(getResources().getColor(index == TAB_MINE ? R.color.global_text_color : R.color.global_message_noselected_color));

        final Fragment lastFragment = currentFragment;

        if (deviceListCallback == null) {
            deviceListCallback = () -> {
                if (deviceListFragment != null) {
                    return deviceListFragment.getDeviceList();
                }
                return null;
            };
        }

        if (index == TAB_HOME) {
            if (deviceListFragment == null) {
                deviceListFragment = DeviceListFragment.newInstance(true, getArguments());
                if (mineFragment != null) {
                    mineFragment.setDeviceListFragment(deviceListFragment);
                }
                transaction.add(R.id.sub_tab_container, deviceListFragment);
            } else {
                deviceListFragment.onTabReShow();
            }
            currentFragment = deviceListFragment;
        } else if (index == TAB_MESSAGE) {
            if (messageFragment == null) {
                messageFragment = MessageFragment.newInstance(getArguments(), false);
                messageFragment.setDeviceListCallback(deviceListCallback);
                transaction.add(R.id.sub_tab_container, messageFragment);
            }
            currentFragment = messageFragment;
        } else if (index == TAB_MINE) {
            if (mineFragment == null) {
                mineFragment = MineFragment.newInstance();
                if (deviceListFragment != null) {
                    mineFragment.setDeviceListFragment(deviceListFragment);
                }
                transaction.add(R.id.sub_tab_container, mineFragment);
            }
            currentFragment = mineFragment;
        }

        if (lastFragment != null && lastFragment != currentFragment) {
            transaction.hide(lastFragment);
        }

        transaction.show(currentFragment);
        transaction.commitAllowingStateLoss();

        StatusNavUtils.setStatusBarColor(getActivity(), statusBarColor);

        currentTabIndex = index;
    }

}
